<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-04
 * Time: 15:15
 */

namespace App\Http\Controllers\Backstage;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\PositionVail;
use App\Model\Common\Position;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PositionController extends Controller
{
    //职务列表
    public function listPosition(Request $request)
    {
        $bId = Auth::user()['u_b_id'];
        $map[] = ['p_bid', $bId];
        $map[] = ['p_status', 1];
        // 判断有没有统计开始时间和结束时间  如果有按时间来查询  如果没有 查询当前月份
        if($request->input('p_name')){
            $map[] = ['p_name', 'like', "%".trim($request->input('p_name'))."%"];
        }

        $list = Position::positionList($map, env('PAGE_LIMIT_ADMIN'));
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    //添加职务
    public function addPosition(Request $request)
    {
        $params = $request->except('token');
        $vali = PositionVail::addPosition($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $params['p_bid'] = Auth::user()['u_b_id'];
        $res = Position::addPosition($params);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功！');
    }
    //修改职务
    public function editPosition(Request $request)
    {
        $params = $request->except('token');
        $vali = PositionVail::editPosition($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $id = $request->input('p_id');
        $p_name = $request->input('p_name');
        $p_authories = $request->input('p_authories');

        $res = Position::editPosition($id, $p_name, $p_authories);
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功！');
    }

    //删除职务
    public function delPosition(Request $request)
    {
        if(!$request->input('p_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $bId = Auth::user()['u_b_id'];
        $res = Position::delPosition($request->input('p_id'), $bId);
        if ($res === -1) {
            return $this->error(Error::POSITION_DOWN_HAVE_USER);
        }
        if (!$res) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功!');
    }

    /**
     * 修改了职务权限以后   在批量修改该职务下面所有员工的权限
     */
    public function editPositionDownUserPermission(Request $request)
    {
        if (!$request->input('id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = Position::editPositionDownUserPermission($request->input('id'));
        if ($res === -1) {
            return $this->error(Error::DEPARTMENT_NO_USER);
        }
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok','修改成功！');

    }

}